package com.touch.jgloo.owt;
import java.awt.Color;
import java.awt.event.*;
import java.util.Vector;

import com.sun.opengl.util.texture.Texture;
import com.touch.graphics.UVType;
import com.touch.jgloo.core.*;
/**
 * JGLPane is a JGLComponent that supports adding other JGLComponents to itself. 
 * Those sub-components can only be drawn on itself and are all rendered with the metho "render(GL)"
 * @author Homicidal Monkey
 * @version 10.1.9
 * @since r419
 * @see com/touch/Licence
 */
public class JGLPane extends JGLContainer implements MouseListener
{	
	//background that defines the JGLPane
	final JGLQuad background;
	//foreground that defines the JGLPane
	final JGLQuad foreground;
	//child components that belong to the JGLPane	
	private final Vector <JGLComponent> components = new Vector <JGLComponent>();
	/**
	 * Create a JGLPane and use the default size of 64x64.
	 * @since r461
	 */
	public JGLPane()
	{
		//create the background object
		background = new JGLQuad(64, 64);
		//create the foreground object
		foreground = new JGLQuad(60, 60);
		//set the size of the window to the default 64x64
		setSize(64, 64);
		//apply the defualt stylesheet to the panel
		this.applyStyleSheet(JGLStyleSheet.DEFAULT_PANEL);
	}
	/**
	 * Creates a JGLPane with defined width and height
	 * @param w Width of the JGLPane
	 * @param h Height of the JGLPane
	 * @since r461
	 */
	public JGLPane(final int w, final int h)
	{
		//create the background object
		background = new JGLQuad(w, h);
		//create the foreground object
		foreground = new JGLQuad(w - 4, h - 4);
		//set the size of the panel
		setSize(w, h);
		//apply the default panel Style Sheet
		this.applyStyleSheet(JGLStyleSheet.DEFAULT_PANEL);
	}
	/**
	 * Adds a JGLComponent to the JGLPanel.
	 * @param component JGLComponent to add
	 * @since r461
	 */
	public final  void add(JGLComponent component)
	{
		//add the JGLComponent to the panel
		this.components.add(component);
	}
	/**
	 * Removes all of the child components from the JGLPane
	 * @since r461
	 */
	public final  void removeComponents()
	{
		//remove all of the elements from the JGLComponent
		this.components.removeAllElements();
	}	
	public final  void resized(final int width, final int height)
	{
		//resize the background
		background.resize(width, height);
		//resize the foreground
		foreground.resize(width - 4, height - 4);
	}
	public final  void repositioned(final int x, final int y, final int z)
	{
		for(JGLComponent component: components)
			component.setParentPosition(
				this.getPositionX(), 
				this.getPositionY(), 
				this.getPositionZ());		
	}
	public final  void render()
	{
		//push the draw-matrix
		GL_INSTANCE.glPushMatrix();
			//translate to the position of the panel (top-left point of panel object)
			GL_INSTANCE.glTranslatef(this.getPositionX(), this.getPositionY(), this.getPositionZ());
			//draw the background
			background.render();
			//push the draw-matrix
			GL_INSTANCE.glPushMatrix();
				//translate an additional (2, 2, 0) to center the foreground
				GL_INSTANCE.glTranslatef(2, 2, 0);
				//draw the foreground
				foreground.render();
			//pop the draw-matrix
			GL_INSTANCE.glPopMatrix();
			for(JGLComponent component: components)
				component.render();
		GL_INSTANCE.glPopMatrix();
	}
	public final  void clean() 
	{
		//clean the background
		background.clean();
		//clean the foreground
		foreground.clean();
		//clean each of the components
		for(JGLComponent component: components)
			component.clean();		
	}
	public final  void compile()
	{
		//compile the background
		background.compile();
		//compile the foreground
		foreground.compile();
		//compile each of the components
		for(JGLComponent component: components)
			component.compile();		
	}	
	@Override
	public final  void applyStyleSheet(final JGLStyleSheet ss)
	{
		background.setColor(ss.backgroundColor);
		background.setTexture(ss.backgroundTexture);
		background.setUV(UVType.QUADMAP);
		foreground.setColor(ss.foregroundColor);
		foreground.setTexture(ss.foregroundTexture);	
		foreground.setUV(UVType.QUADMAP);		
	}
	/**
	 * Sets the background of the panel
	 * @param texture Texture to set the background as
	 * @since r461
	 */
	public final void setBackground(final Texture texture)
	{
		//set the Texture of the foreground 
		foreground.setTexture(texture);
		//set the color of the foreground
		foreground.setColor(Color.WHITE);
	}
	/** 
	 * Centers the panel in the middle of the screen.
	 * @since r461
	 */
	public final  void center()
	{	
		//reconfigure the position of the window to the center.
		//(half the window size, subtract the width/height)
		this.setPosition(320 - this.getWidth() / 2, 240 - this.getHeight() / 2, this.getPositionZ());		
	}
	@Override
	public final void setVisible(boolean v)
	{
		super.setVisible(v);
		//set each component to the same visibility
		for(JGLComponent component: this.components)
			component.setVisible(v);
	}
	public JGLPane clone()
	{
		final JGLPane clone = new JGLPane(this.getWidth(), this.getHeight());
		
		clone.background.setTexture((Texture)this.background.getPolygon().getTexture());
		clone.foreground.setTexture((Texture)this.foreground.getPolygon().getTexture());
		clone.setPosition(this.getPositionX(), this.getPositionY(), this.getPositionZ());
		clone.setParentPosition(this.getParentPosition().x, this.getParentPosition().y, this.getParentPosition().z);
		clone.setVisible(this.isVisible());
		
		return clone;
	}
}
